package com.cmwa.solution.business.product.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.product.entity.TreFeeDetailVo;
import com.cmwa.solution.business.product.entity.TreFeeRateChangeVo;
import com.cmwa.solution.business.product.service.TreFeeDetailService;
import com.cmwa.solution.business.product.service.TreFeeRateChangeService;
import com.cmwa.solution.business.treYield.entity.TreYieldVo;
import com.cmwa.solution.business.treYield.service.TreYieldService;
import com.cmwa.solution.business.treYieldRateChange.entity.TreYieldRateChangeVo;
import com.cmwa.solution.business.treYieldRateChange.service.TreYieldRateChangeService;
import com.cmwa.solution.exception.ValidateFailedException;
import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.utils.JsonUtil;
import com.cmwa.solution.utils.SessionUtils;
import com.cmwa.solution.utils.cached.DictionaryCached;

/**
 * 费率类型管理控制器
 * 
 * @author ex-chenxh
 * @date 2019-01-22 10:30:12
 */
@Controller
@RequestMapping("/business/treFeeDetail")
public class TreFeeDetailController extends BaseController {

	private static final Log log = LogFactory.getLog(TreFeeDetailController.class);

	private final static String JSP_PATH = "business/product/";
	@Resource
	private TreFeeDetailService treFeeDetailService;
	@Resource
	private TreFeeRateChangeService treFeeRateChangeService;
	@Resource
	private TreYieldService treYieldService;
	@Resource
	private TreYieldRateChangeService treYieldRateChangeService;

	/**
	 * 跳转到列表页面
	 * 
	 * @author ex-chenxh
	 * @date 2019-01-22 10:30:12
	 * @param model
	 * @return
	 */
	@RequestMapping("/treFeeDetailListView")
	public String treFeeDetailListView(ModelMap model) {
		log.info("前往费率类型管理列表页面");
		return JSP_PATH + "treFeeDetailList";
	}

	/**
	 * 通过条件查询数据并分页
	 * 
	 * @param param
	 * @return
	 */
	@RequestMapping("/TreFeeDetailListPage")
	@ResponseBody
	public Page<TreFeeDetailVo> TreFeeDetailListPage(SearchParam param) {
		log.info("费率类型管理列表页面加载分页数据");
		return treFeeDetailService.TreFeeDetailListPage(param);
	}

	/**
	 * 前往费率类型新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/treFeeDetailView")
	public String treFeeDetailView(@RequestParam("type") String type, ModelMap model) throws ValidateFailedException {
		log.info("前往费率类型编辑页面！");
		List<DictionaryVo> dictionaryList = null;
		TreFeeDetailVo treFeeDetailVo = null;

		// 加载拓展字段下拉选项
		dictionaryList = DictionaryCached.getDictionaryByType("TreFeeDetail_TYPE");
		// 设置前端要显示的数据
		model.addAttribute("treFeeDetailVo", treFeeDetailVo);
		model.addAttribute("dictionaryList", dictionaryList);
		model.addAttribute("type", type);
		return JSP_PATH + "treFeeDetailView";
	}
	/**
	 * 前往费率类型新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/treFeeDetailDelete")
	public String treFeeDetailDelete(ModelMap model) throws ValidateFailedException {
		List<DictionaryVo> dictionaryList = null;
		dictionaryList = DictionaryCached.getDictionaryByType("TreFeeDetail_TYPE");
		model.addAttribute("dictionaryList", dictionaryList);
		return JSP_PATH + "treFeeDetailDelete";
	}
	/**
	 * 前往费率类型新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/deleteTreFeeDetailInfo")
	public String deleteTreFeeDetailInfo(@RequestParam("subPrdCode")String subPrdCode,@RequestParam("feeType")String feeType,String feeCode,ModelMap model) throws ValidateFailedException {
		log.info("删除费率类型信息！");
		if(StringUtils.isNotBlank(subPrdCode)&&StringUtils.isNotBlank(feeType)) {
			if("fee".equals(feeType)) {//删除费率
				if(StringUtils.isNotBlank(feeCode)) {//费用管理类型
					treFeeDetailService.deleteTreFeeRate(subPrdCode, feeCode);
					return redirectSuccess();
				}else {
					log.error("删除费用费率时，费率类型不能为空！");
				}
			}else {//删除收益
				treYieldService.deleteYieldRate(subPrdCode);
				return redirectSuccess();
			}
		}else {
			log.error("删除费用费率或收益时，参数不能为空！");
		}
		return redirectExecFaild();
	}

	/**
	 * 新增费率类型信息
	 * 
	 * @param TreFeeDetailVo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveTreFeeDetail", method = { RequestMethod.POST })
	public String saveTreFeeDetail(TreFeeDetailVo treFeeDetailVo, @RequestParam(value = "feeChangeListJson", required = false) String feeChangeListJson, ModelMap model) throws ValidateFailedException {
		log.info("新增费率类型信息");
		// 前台传递过来的对象不能为空
		if (null != treFeeDetailVo) {
			// 将费率类型存入数据库中
			treFeeDetailService.insertTreFeeDetail(treFeeDetailVo);
			String feeRateType = treFeeDetailVo.getFeeRateType();
			// 保存浮动费率信息
			if (StringUtils.isNotBlank(feeRateType) && "1".equals(feeRateType)) {
				String feeId = treFeeDetailVo.getFeeId();
				List<TreFeeRateChangeVo> feeRateChangeList = JsonUtil.parseListObject(feeChangeListJson, TreFeeRateChangeVo.class);
				if (CollectionUtils.isNotEmpty(feeRateChangeList)) {
					for (TreFeeRateChangeVo changeVo : feeRateChangeList) {
						changeVo.setFeeRate(changeVo.getChangeFeeRate());
						changeVo.setFeeId(feeId);
						treFeeRateChangeService.insertTreFeeRateChange(changeVo);
					}
				}
			}
			return redirectSuccess();
		} else {
			log.error("新增失败，参数treFeeDetailVo不能为空:" + treFeeDetailVo);
			model.addAttribute("exception", "新增失败，参数不能为空");
			return redirectExecFaild();
		}
	}

	@RequestMapping(value = "/saveTreYieldDetail", method = { RequestMethod.POST })
	public String saveTreYieldDetail(TreYieldVo treYieldVo, @RequestParam(value = "yieldChangeListJson", required = false) String yieldChangeListJson, ModelMap model) throws ValidateFailedException {
		log.info("新增客户收益信息");
		String createId = SessionUtils.getEmployee().getID();
		// 前台传递过来的对象不能为空
		if (null != treYieldVo) {
			// 将费率类型存入数据库中
			treYieldService.insertTreYield(treYieldVo);
			String yieldType = treYieldVo.getYieldType();
			// 保存浮动费率信息
			if (StringUtils.isNotBlank(yieldType) && "1".equals(yieldType)) {
				String prdYieldId = treYieldVo.getPrdYieldId();
				List<TreYieldRateChangeVo> list = JsonUtil.parseListObject(yieldChangeListJson, TreYieldRateChangeVo.class);
				for (TreYieldRateChangeVo treYieldRateChangeVo : list) {
					treYieldRateChangeVo.setCreateId(createId);
					treYieldRateChangeVo.setPrdYieldId(prdYieldId);
				}
				treYieldRateChangeService.batchAddYieldRateChange(list);
			}
			return redirectSuccess();
		} else {
			log.error("新增失败，参数treYieldVo不能为空:" + treYieldVo);
			model.addAttribute("exception", "新增失败，参数不能为空");
			return redirectExecFaild();
		}
	}

	/**
	 * 修改费率类型信息
	 * 
	 * @param TreFeeDetailVo
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateTreFeeDetail", method = { RequestMethod.POST })
	public String updateTreFeeDetail(TreFeeDetailVo treFeeDetailVo, ModelMap model) throws Exception {
		log.info("修改费率类型信息");
		// 前台传递过来的集合不能为空
		if (null != treFeeDetailVo) {
			// 将费率类型存入数据库中
			treFeeDetailService.updateTreFeeDetail(treFeeDetailVo);
			return redirectSuccess();
		} else {
			log.error("修改失败，参数不能为空");
			model.addAttribute("exception", "修改失败，参数不能为空");
			return redirectExecFaild();
		}
	}

	/**
	 * 删除费率类型信息
	 * 
	 * @param TreFeeDetailVo
	 * @param model
	 * @return
	 * @throws ValidateFailedException
	 */
	@RequestMapping("/deleteTreFeeDetail")
	@ResponseBody
	public Result<Object> deleteTreFeeDetail(@RequestParam("feeId") String feeId, ModelMap model) throws ValidateFailedException {
		log.info("根据主键删除费率类型信息，feeId：" + feeId);

		Result<Object> result = new Result<Object>();
		boolean isComplete = false;
		// 判断需要删除的主键是否为空
		if (StringUtils.isNotBlank(feeId)) {
			int status = treFeeDetailService.deleteTreFeeDetail(feeId);
			log.info("根据主键删除相关记录数：" + status);
			if (status > 0) {
				isComplete = true;
			}
		} else {
			log.error("需要删除的id不能为空！");
			result.setMessage("需要删除的主键不能为空！");
		}
		result.setSuccess(isComplete);
		return result;
	}
}
